package com.party.core.service.mall.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.core.dao.read.mall.MallStoreResourcesReadDao;
import com.party.core.dao.write.mall.MallStoreResourcesWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.mall.MallStoreResources;
import com.party.core.service.mall.IMallStoreResourcesService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 商城店铺资源
 * Created by yifeng
 *
 * @date 2018/4/3 0003
 * @time 上午 9:52
 */
@Service
public class MallStoreResourcesService implements IMallStoreResourcesService {
    @Autowired
    private MallStoreResourcesReadDao mallStoreResourcesReadDao;

    @Autowired
    private MallStoreResourcesWriteDao mallStoreResourcesWriteDao;

    /**
     * 商城店铺资源插入
     *
     * @param mallStoreResources 商城店铺资源
     * @return 编号
     */
    @Override
    public String insert(MallStoreResources mallStoreResources) {
        BaseModel.preInsert(mallStoreResources);
        boolean result = mallStoreResourcesWriteDao.insert(mallStoreResources);
        if (result) {
            return mallStoreResources.getId();
        }
        return null;
    }

    /**
     * 更新商城店铺资源
     *
     * @param mallStoreResources 商城店铺资源
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(MallStoreResources mallStoreResources) {
        mallStoreResources.setUpdateDate(new Date());
        return mallStoreResourcesWriteDao.update(mallStoreResources);
    }

    /**
     * 逻辑删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallStoreResourcesWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallStoreResourcesWriteDao.delete(id);
    }

    /**
     * 批量逻辑删号
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallStoreResourcesWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量物理删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallStoreResourcesWriteDao.batchDelete(ids);
    }

    /**
     * 获取店铺资源
     *
     * @param id 主键
     * @return 店铺资源
     */
    @Override
    public MallStoreResources get(String id) {
        return mallStoreResourcesReadDao.get(id);
    }

    /**
     * 分页查询
     *
     * @param mallStoreResources 店铺资源
     * @param page               分页信息
     * @return 分页列表
     */
    @Override
    public List<MallStoreResources> listPage(MallStoreResources mallStoreResources, Page page) {
        return mallStoreResourcesReadDao.listPage(mallStoreResources, page);
    }

    /**
     * 查询所有
     *
     * @param mallStoreResources 店铺资源
     * @return 分页列表
     */
    @Override
    public List<MallStoreResources> list(MallStoreResources mallStoreResources) {
        return mallStoreResourcesReadDao.listPage(mallStoreResources, null);
    }

    @Override
    public List<MallStoreResources> batchList(@NotNull Set<String> ids, MallStoreResources mallStoreResources, Page page) {
        return mallStoreResourcesReadDao.batchList(ids, new HashMap<String, Object>(), page);
    }

    @Override
    public List<MallStoreResources> findByStoreId(String storeId, Integer model) {
        MallStoreResources resources = new MallStoreResources();
        resources.setStoreId(storeId);
        resources.setModel(model);
        return mallStoreResourcesReadDao.listPage(resources, null);
    }

    @Override
    public List<MallStoreResources> findByStoreId(String storeId, Integer model, Page page) {
        MallStoreResources resources = new MallStoreResources();
        resources.setStoreId(storeId);
        resources.setModel(model);
        return mallStoreResourcesReadDao.listPage(resources, page);
    }

    @Override
    public Integer findByTargetId(String targetId) {
        return mallStoreResourcesReadDao.findByTargetId(targetId);
    }

    @Override
    public void deleteByTargetId(String targetId) {
        mallStoreResourcesWriteDao.deleteByTargetId(targetId);
    }
}
